﻿using Grasshopper.Kernel;
using System.Collections.Generic;
using System.Linq;

namespace AiMoInGH.Component
{
    static class AiMoComponentsHelper
    {
        public static string[] GetAllSharingCodeAiMoComponentsDlls(AiMoInGHComponent component)
        {
            List<string> result = new List<string>();
            GH_Document document = component.OnPingDocument();
            if (document == null) return result.ToArray();
            foreach (AiMoInGHComponent aimoComponent in document.Objects.Where(x => x.Attributes.IsTopLevel && x is AiMoInGHComponent).Cast<AiMoInGHComponent>().Where(x => x.HasSharedPublicClass))
            {

            }

            return result.ToArray();
            //return document.Objects.Where(x => x.Attributes.IsTopLevel && x is AiMoInGHComponent).Cast<AiMoInGHComponent>().Where(x=>x.HasSharedPublicClass).Select(x=>x.CurrentDllFilePath).ToArray();
        }

        public static AiMoInGHComponent[] GetAllRecepients(AiMoInGHComponent component)
        {
            List<AiMoInGHComponent> allComponents = new List<AiMoInGHComponent>() { component };
            GetReceipents(component, allComponents);
            return allComponents.ToArray();
        }

        static void GetReceipents(IGH_Component currentComponent, List<AiMoInGHComponent> allComponents)
        {
            foreach (IGH_Param param in currentComponent.Params.Output)
            {
                GetReceipents(param, allComponents);
            }
        }
        static void GetReceipents(IGH_Param currentParam, List<AiMoInGHComponent> allComponents)
        {
            foreach (IGH_Param recipientInputParam in currentParam.Recipients)
            {
                IGH_DocumentObject documentObject = recipientInputParam.Attributes.GetTopLevel.DocObject;
                if (documentObject is AiMoInGHComponent recipientAiMoComponent)
                {
                    allComponents.Add(recipientAiMoComponent);
                    GetReceipents(recipientAiMoComponent, allComponents);
                }
                else if (documentObject is IGH_Component recipientComponent)
                {
                    GetReceipents(recipientComponent, allComponents);
                }
                else if (documentObject is IGH_Param ownerParam)
                {
                    GetReceipents(ownerParam, allComponents);
                }
            }
        }
    }
}
